-- Copyright (C) 2013 wgm.china@gmail.com (王光明)
--vss.list.utils为所有list方法的公共函数

local strfind 		= string.find
local strsub  		= string.sub
local strbyte 		= string.byte
local strrep  		= string.rep
local strchar 		= string.char
local strrep 		= string.rep
local strlen 		= string.len
local type   		= type
local pairs   		= pairs
local ngx	  		= ngx 
local tb_insert		= table.insert
local tonumber		= tonumber

-------1定义公共组件(开始)---------------
local config_table = ngx.shared.gcs_config  					--定义gcs_config缓存
local db           = require("gcs.db")
local utils     = require "gcs.utils"
-------1定义公共组件（结束）----------------
-------2加载配置文件（开始）----------------

-------2加载配置文件（结束）----------------
module(...)
local version = '1.1'

--get version 中保存的版本号
function get_version()
	return version
end

--返回连接队列数据库
local function get_list_db()
	local list_db,err = db.redis_connect()
	return list_db,err
end

--关闭队列数据库
function close_list_db(list_db)
	return db.redis_close(list_db)
end

function push(lr,list_name,id)
	local list_db,err = get_list_db()
	if not list_db then return nil,err end
	local res ,err 
	if lr == "l" then
		res,err = list_db:lpush(list_name,id)
	else
		res,err = list_db:rpush(list_name,id)
	end	
	close_list_db(list_db)
	return res,err
end

function pop(lr,list_name)
	local list_db,err = get_list_db()
	if not list_db then return nil,err end
	local res,err
	if lr == "l" then
	--utils.gcs_log("debug","list_utils pop start")
		res,err = list_db:lpop(list_name)
		--utils.gcs_log("debug","list_utils pop end")
	else
	--utils.gcs_log("debug","list_utils pop start")
	    res,err = list_db:rpop(list_name)
	    --utils.gcs_log("debug","list_utils pop end")
	end
	if res == ngx.null then
		return 0,err
	end
	close_list_db(list_db)
	return res,err		
end

function count(list_name)
	local list_db,err = get_list_db()
	if not list_db then return nil,err end
	local res,err
	local tb_res = {}
	res,err = list_db:llen(list_name)
	if res == ngx.null or not res then res = 0 end
	close_list_db(list_db)
	return res,err
end

function set(data)
	local list_db,err = get_list_db()
	if not list_db then return nil,err end
	local res ,err 
	for key,value in pairs(data) do
		res,err = list_db:set(key,value)
		if not res then return nil,err end
	end
	close_list_db(list_db)
	return res,err
end

function get(data)
	local list_db,err = get_list_db()
	if not list_db then return nil,err end
	local res ,err 
	local tb_res = {}
	for _,key in pairs(data) do
		res,err = list_db:get(key)
		if not res then return nil,err end
		if res == ngx.null then res = "" end
		tb_res[key] = res
	end
	close_list_db(list_db)
	return tb_res,err
end

function del(data)
	local list_db,err = get_list_db()
	if not list_db then return nil,err end
	local res ,err 
	local tb_res = {}
	for _,key in pairs(data) do
		res,err = list_db:del(key)
		if not res then return nil,err end
		if res == ngx.null then res = "" end
		tb_res[key] = res
	end
	close_list_db(list_db)
	return tb_res,err
end

function get1(data)
	local list_db,err = get_list_db()
	if not list_db then return nil,err end
	local res ,err 
	local tb_res = {}
	for _,key in pairs(data) do
		res,err = list_db:get(key)
		if not res then return nil,err end
		if res == ngx.null then res = 0 end
		tb_res[key] = res
	end
	close_list_db(list_db)
	return tb_res,err
end

function incr(data)
	local list_db,err = get_list_db()
	if not list_db then return nil,err end
	local res ,err 
	local tb_res = {}
	for key,value in pairs(data) do
		value = tonumber(value) or 0
		res,err = list_db:incrby(key,value)
		if not res then return nil,err end
		tb_res[key] = res
	end
	close_list_db(list_db)
	return tb_res,err
end


         
function exists(data)
  local list_db,err = get_list_db()
  if not list_db then return nil,err end
  local res ,err 
  local tb_res = {}
  for _,key in pairs(data) do
    res,err = list_db:exists(key)
    if not res then return nil,err end
    tb_res[key] = res
  end
  close_list_db(list_db)
  return tb_res,err
end



